/**
 * Copyright (C) 2015 Infinite Automation Software. All rights reserved.
 * @author Terry Packer
 */
package com.serotonin.m2m2.db.upgrade;

import java.util.HashMap;
import java.util.Map;

import com.serotonin.m2m2.db.DatabaseProxy;
/**
 * Upgrade to add template system
 * 
 * @author Terry Packer
 *
 */
public class Upgrade11 extends DBUpgrade {

    @Override
    public void upgrade() throws Exception {
        // Run the script.
        Map<String, String[]> scripts = new HashMap<>();
        scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyScript);
        scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlScript);
        scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlScript);
        scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2Script);
        runScript(scripts);

    }

    @Override
    protected String getNewSchemaVersion() {
        return "12";
    }

    private final String[] mssqlScript = { //
    	"CREATE TABLE jsonData (id int not null identity, xid nvarchar(50) not null, name nvarchar(255) not null, readPermission varchar(255), editPermission varchar(255), data ntext, primary key (id) )engine=InnoDB;",
    	"ALTER TABLE jsonData ADD CONSTRAINT jsonDataUn1 UNIQUE (xid);"
    };
    private final String[] derbyScript = { //
    	"CREATE TABLE jsonData (id int not null generated by default as identity (start with 1, increment by 1), xid varchar(50) not null, name varchar(255) not null, readPermission varchar(255), editPermission varchar(255), data clob);",
    	"ALTER TABLE jsonData ADD CONSTRAINT jsonDataUn1 UNIQUE (xid);"
    };    
    private final String[] mysqlScript = { //
    	"CREATE TABLE jsonData (id int not null auto_increment,xid varchar(50) not null, name varchar(255) not null, readPermission varchar(255), editPermission varchar(255), data longtext, primary key (id))engine=InnoDB;",
    	"ALTER TABLE jsonData ADD CONSTRAINT jsonDataUn1 UNIQUE (xid);"
    };    

    private final String[] h2Script = { //
    	"CREATE TABLE jsonData (id int not null auto_increment, xid varchar(50) not null, name varchar(255) not null, readPermission varchar(255), editPermission varchar(255), data longtext, primary key (id));",
    	"ALTER TABLE jsonData ADD CONSTRAINT jsonDataUn1 UNIQUE (xid);"
    };

}
